import '../../util/ustil.js'
// pages/pay/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //地址信息
    address: {},
    //结算的商品
    checkedGoods: [],
    //结算商品总价
    total: 0,
    //结算商品总数
    goodsNum: 0,
    //控制二维码显示与隐藏
    show_img: false,
    //显示输入模态框的显示与隐藏
    hiddenModalInput: true,
    //输入的值
    value: ''
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setCart()
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //清空数据
    this.setData({
      address: {},
      checkedGoods: [],
      total: 0,
      goodsNum: 0
    })
    wx.showLoading({
      title: '加载中',
      mask: true,
      direction: 1200
    });
    setTimeout(() => {
      //停止下拉刷新
      wx.stopPullDownRefresh()
      wx.hideLoading(); //隐藏加载动画
      //重新赋值
      this.setCart()
    }, 1200)
  },
  setCart() {
    //获取缓存中的地址还有商品信息
    const address = wx.getStorageSync('address');
    const checkedGoods = wx.getStorageSync('checkedGoods') || [];
    //计算总价和总量
    let total = 0
    let goodsNum = 0
    checkedGoods.forEach(v => {
      total += parseFloat(v.goods_price) * parseInt(v.num)
      goodsNum += v.num
    })
    //赋给data中的变量
    this.setData({
      address,
      checkedGoods,
      total,
      goodsNum
    })
  },
  //点击支付触发
  handlePay() {
    //获取缓存中的token
    const token = wx.getStorageSync('token');
    if (!token) {
      //没有token值，跳转授权页面
      wx.navigateTo({
        url: '/pages/auth/index'
      });
      setTimeout(() => {
        this.setData({
          show_img: !this.data.show_img
        })
      }, 1500)
      return;
    }
    //获取缓存中的结算数据
    const checkedGoods = wx.getStorageSync('checkedGoods') || []
    //判断是否有数据
    if (checkedGoods.length === 0) {
      wx.showToast({
        title: '(◞‸◟ )没有需要结算的商品',
        icon: 'none',
        duration: 1200,
        mask: false
      });
      return;
    }
    //有商品，有token，显示支付二维码
    this.setData({
      show_img: !this.data.show_img
    })
  },
  //取消支付
  cancelPay() {
    this.setData({
      show_img: !this.data.show_img
    })
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    setTimeout(() => {
      wx.hideLoading();
      wx.showToast({
        title: '支付失败',
        icon: 'none',
        image: '',
        duration: 1500,
        mask: false,
      });
    }, 1500)
  },
  //确认支付
  surePay() {
    this.setData({
      hiddenModalInput: !this.data.hiddenModalInput
    })
  },
  //取消按钮
  cancel: function () {
    this.setData({
      hiddenModalInput: true
    })
  },
  //确认按钮
  confirm: function () {
    //判断输入的值是否相等
    if (this.data.value === "123456") {
      wx.showLoading({
        title: '支付中',
        mask: true
      });
      setTimeout(() => {
        wx.hideLoading();
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 1500,
          mask: false,
        });
        //先获取结算的商品数据
        const checkedGoods = wx.getStorageSync('checkedGoods');
        //获取缓存中的订单数据
        const ordersList = wx.getStorageSync('ordersList') || []
        checkedGoods.forEach(v => {
          // 生成订单
          let order = {}
          //订单编号
          const platform = 'GD'
          let r1 = Math.floor(Math.random() * 10)
          let r2 = Math.floor(Math.random() * 10)
          let r3 = Math.floor(Math.random() * 10)
          let sysDate = new Date().Format('yyyyMMddhhmmss')
          order.orderId = platform + r1 + sysDate + r2 + r3
          order.createTime = new Date().Format('yyyy-MM-dd hh:mm:ss')
          order.orderPrice = parseFloat(v.goods_price)
          ordersList.push(order)
        })
        wx.setStorageSync('ordersList', ordersList);
        //移除存储
        wx.removeStorageSync('checkedGoods');
        //把购物车中勾选的商品删除
        let newCarts = wx.getStorageSync('carts') || [];
        let carts = newCarts.filter(v => !v.checked)
        wx.setStorageSync('carts', carts);
        this.setData({
          hiddenModalInput: true,
          show_img: false,
          checkedGoods: [],
          total: 0,
          goodsNum: 0,
        })
      }, 1500)
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/order/index'
        });
      }, 2000)

    } else {
      wx.showToast({
        title: '( ´•︵•` )密码有误！',
        icon: 'none',
        duration: 1500,
        mask: false,
      });
    }

  },
  //获取输入的值
  checkInput(e) {
    //获取输入的值
    const {
      value
    } = e.detail
    this.setData({
      value
    })
  },
  //关闭遮罩
  closeModel() {
    this.setData({
      show_img: false
    })
  }
})